package com.easybuilder.core.rbac.service.impl;

import com.easybuilder.blog.service.ArticleService;
import com.easybuilder.blog.service.FansService;
import com.easybuilder.common.annotation.Transactional;
import com.easybuilder.common.base.entity.BaseEntity;
import com.easybuilder.common.base.entity.PageQueryWrapper;
import com.easybuilder.common.base.entity.PageResultWrapper;
import com.easybuilder.common.utils.*;
import com.easybuilder.core.rbac.dao.SysUserDao;
import com.easybuilder.core.rbac.dto.SysUserDTO;
import com.easybuilder.core.rbac.entity.SysRole;
import com.easybuilder.core.rbac.entity.SysUser;
import com.easybuilder.core.rbac.service.SysRoleService;
import com.easybuilder.core.rbac.service.SysUserService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author ZXS
 * @version 1.0
 * @description 类
 * @date 2021-5-25 22:51
 */
@Service
public class SysUserServiceImpl implements SysUserService {
    @Resource
    private SysUserDao sysUserDao;
    @Autowired
    private SysRoleService roleService;
    @Autowired
    private FansService fansService;
    @Autowired
    private ArticleService articleService;

    @Override
    public PageResultWrapper<SysUser> page(PageQueryWrapper<SysUser> queryWrapper) {
        Page page = PageHelper.startPage(queryWrapper.getPageNum(), queryWrapper.getPageSize());
        Example example = PageUtil.buildExample(queryWrapper.getSearchEntity(),SysUser.class);
        example.orderBy("createTime").desc();
        sysUserDao.selectByExample(example);
        return PageUtil.build(page);
    }

    @Override
    @Transactional
    public Long insert(BaseEntity entity) {

        entity = new BaseEntity("sys_user");
        entity.put("username", "赵六").put("password", "456").put("gender",1)
                .put("age", 20).put("phone", "15555555").put("email", "123@123.com");
        long insert = DBUtil.insert(entity);
        String s = null;
        System.out.println(s.length());
        System.out.println("操作结果：" + insert);
        return null;
    }

    /** 根据用户id集合，批量获取用户 */
    @Override
    public List<SysUser> selectByUserIds(List<Long> userIds) {
        Example example = new Example(SysUser.class);
        example.createCriteria().andIn("id", userIds);
        return sysUserDao.selectByExample(example);
    }



    /** 重写UserDetailsService接口方法，根据用户名查找用户*/
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        SysUser sysUser = new SysUser();
        sysUser.setUsername(s);
        SysUser user = sysUserDao.selectOne(sysUser);
        if (user == null) {
            return null;
        }
        List<SysRole> roles = roleService.getByUserId(user.getId());
        user.setRoles(roles);
        return user;
    }

    /** 插入一条用户记录 */
    @Override
    public int insert(SysUser sysUser) {

        return sysUserDao.insertSelective(sysUser);
    }

    /** 统计用户数量 */
    @Override
    public List<Integer> countUser() {
        String date = TimeUtil.getFormatDay(new Date());
        return sysUserDao.countUser(date);
    }

    /** 根据用户id获取用户信息 */
    @Override
    public SysUser getUserById(Long id) {
        if (id == null) {
          return null;
        }
        SysUser sysUser = new SysUser();
        sysUser.setId(id);
        return sysUserDao.selectByPrimaryKey(sysUser);
    }

    /** 获取当前登录用户信息 */
    @Override
    public SysUserDTO getCurrentUserInfo() {
        // 获取当前登录用户
        SysUser user = UserUtil.getUser();
        if (user != null) {
            SysUserDTO userDTO = new SysUserDTO();
            BeanUtils.copyProperties(user, userDTO);
            // 设置其他信息
            setOtherInfo(userDTO);
            return userDTO;
        }
        return null;
    }

    /** 保存用户数据 */
    @Override
    public void save(SysUser sysUser) {
        if (sysUser.getId() == null) { //新增
            // 设置密码
            sysUser.setPassword(PasswordUtil.initPassword());
            sysUser.setCreateTime(new Date());
            sysUserDao.insertSelective(sysUser);
        } else {
            sysUser.setUpdateTime(new Date());
            sysUserDao.updateByPrimaryKeySelective(sysUser);
        }
    }

    @Override
    public void deleteById(Long id) {
        SysUser sysUser = new SysUser();
        sysUser.setId(id);
        sysUserDao.deleteByPrimaryKey(sysUser);
    }

    /** 设置登录用户其他信息 */
    private void setOtherInfo(SysUserDTO userDTO) {
        // 设置粉丝数量
        setFansCount(userDTO);
        // 设置关注数量
        setAttentionCount(userDTO);
        // 设置作品数量
        setWorksCount(userDTO);
        //TODO 设置收藏数量
        userDTO.setCollectCount(0L);
    }

    /** 设置粉丝数量 */
    private void setFansCount(SysUserDTO userDTO) {
        Long fansCount = fansService.countFans(userDTO.getId());
        userDTO.setFansCount(fansCount);
    }

    /** 设置关注数量 */
    private void setAttentionCount(SysUserDTO userDTO) {
        Long attentionCount = fansService.countAttention(userDTO.getId());
        userDTO.setAttentionCount(attentionCount);
    }

    /** 设置作品数量 */
    private void setWorksCount(SysUserDTO userDTO) {
        Long worksCount = articleService.countArticleByUserId(userDTO.getId());
        userDTO.setWorksCount(worksCount);
    }




}
